package com.luocc.ccdemo.db

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.luocc.ccdemo.entity.Video
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors

@Database(entities = [Video::class], version = 1, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {

    abstract fun videoDao(): VideoDao

    companion object {
        val databaseWriteExecutor: ExecutorService = Executors.newFixedThreadPool(4)

        @Volatile
        private var INSTANT: AppDatabase? = null
        fun getDatabase(context: Context): AppDatabase {
            if (INSTANT == null) {
                synchronized(AppDatabase::class.java) {
                    if (INSTANT == null) {
                        INSTANT = Room.databaseBuilder(
                            context.applicationContext,
                            AppDatabase::class.java,
                            "cc_demo"
                        ).build()
                    }
                }
            }
            return INSTANT!!
        }
    }
}